package com.qqtech.qquan.delivery.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.util.HttpClientUtil;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.delivery.enums.DeliveryOrderDictTypeConstants;
import com.qqtech.qquan.delivery.enums.DeliveryOrderStatusDictEnum;
import com.qqtech.qquan.delivery.model.DeliveryOrder;
import com.qqtech.qquan.delivery.model.DeliveryOrderQuery;
import com.qqtech.qquan.delivery.model.DeliveryOrderVo;
import com.qqtech.qquan.delivery.model.DeliverySysDictVo;
import com.qqtech.qquan.delivery.service.DeliveryOrderService;
import com.qqtech.qquan.delivery.service.DeliverySysDictService;
import com.qqtech.qquan.order.enums.OrderDeliveryTypeDictEnum;
import com.qqtech.qquan.order.model.OrderBase;
import com.qqtech.qquan.order.model.OrderDelivery;
import com.qqtech.qquan.order.service.OrderBaseService;
import com.qqtech.qquan.order.service.OrderDeliveryService;
import com.qqtech.qquan.sys.controller.BaseAdminController;
import com.qqtech.qquan.user.service.UserBaseService;

@Controller
@RequestMapping(value = "/delivery/order")
public class DeliveryOrderController extends BaseAdminController {
	@Resource
	private DeliveryOrderService deliveryOrderService;
	
	@Resource
	private OrderBaseService orderBaseService;
	
	@Resource
	private OrderDeliveryService orderDeliveryService;
	
	@Resource
	private UserBaseService userBaseService;
	
	@Resource
	private DeliverySysDictService deliverySysDictService;
    
	@Override
	protected BaseService<DeliveryOrder> getBaseService() {
		return deliveryOrderService;
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/delivery/order-list";
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(DeliveryOrderQuery query,
			HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		if (query == null) {
			query = new DeliveryOrderQuery();
		}
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(),query.getSize(), new PageOrder(
				Direction.DESC, "id"));
		Page<DeliveryOrderVo> page = deliveryOrderService.queryPageList(
				query, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements()+""));
		return adminPage;
	}
	
	@RequestMapping(value = "/detail")
	public String detail(DeliveryOrderQuery deliveryOrderQuery, Model model) {
		DeliveryOrderVo deliveryOrder = null;
		if (deliveryOrderQuery.getId() != null) {
			deliveryOrder = deliveryOrderService.getById(deliveryOrderQuery.getId());
		} else {
			deliveryOrder = deliveryOrderService.getByOrderNo(deliveryOrderQuery.getOrderNo());
		}
		if (deliveryOrder != null) {
			OrderDelivery delivery = orderDeliveryService.getByOrderNoAndType(deliveryOrder.getOrderNo(),
					OrderDeliveryTypeDictEnum.SELLERSERVICE.getCode());
			OrderBase order = orderBaseService.getByOrderNo(deliveryOrder.getOrderNo());
			model.addAttribute("deliveryOrder", deliveryOrder);
			model.addAttribute("order", order);
			model.addAttribute("buyer", userBaseService.getById(order.getBuyerId()));
			model.addAttribute("delivery", delivery);
		}
		return "/delivery/order-detail";
	}
	
	@RequestMapping(value = "/kuaidi", method = RequestMethod.POST)
	@ResponseBody
	public Result kuaidi(DeliveryOrderVo deliveryOrder) {
		Result result = new Result();
		DeliverySysDictVo logistics = null;
		if (deliveryOrder == null) { 
			result.addErr("参数不能为空");
			return result;
		}
		String deliveryName = deliveryOrder.getDeliveryName();
		String deliveryNo = deliveryOrder.getDeliveryNo();
		if (StringUtil.isBlank(deliveryName)) {
			result.addErr("物流公司不能为空");
		} else {
			logistics = deliverySysDictService.getLogistics(deliveryName);
			if (logistics == null) {
				result.addErr("系统不存在这个物流公司");
			}
		}
		if (StringUtil.isBlank(deliveryNo)) {
			result.addErr("物流号不能为空");
		}
		if (result.checkForm()) {
			//?id=XXXX&com=tiantian&nu=11111&show=2&muti=1&order=desc
			String requestBody = "id=" + DeliveryOrderDictTypeConstants.KUAIDI100_KEY + "&com=" + logistics.getCode()
					+ "&nu=" + deliveryNo + "&show=2&muti=1&order=desc";
			String url = DeliveryOrderDictTypeConstants.KUAIDI100_URL + "?" + requestBody;
			String content = HttpClientUtil.send(url, null);
			result.put("content", content);
		}
		return result;
	}
	
	/**
	 * 入库
	 * @param deliveryOrder
	 * @return
	 */
	@RequestMapping(value = "/inStorage", method = RequestMethod.POST)
	@ResponseBody
	public Result inStorage(Integer id) {
		Result result = new Result();
		DeliveryOrderVo deliveryOrder = null;
		if (id == null) {
			result.addErr("id为空");
		} else {
			deliveryOrder = deliveryOrderService.getById(id);
			if (deliveryOrder == null) {
				result.addErr("数据不存在");
			} else if (deliveryOrder.getStatus() != DeliveryOrderStatusDictEnum.NoStorage.getCode()) {
				result.addErr("只有状态是待入库，才能入库");
			}
		}
		if (result.checkForm()) {
			/*deliveryOrder.setStatus(DeliveryOrderStatusDictEnum.NoSign.getCode());
			deliveryOrder.setStatusAt(TimeUtil.now());
			deliveryOrder.setUpdAt(deliveryOrder.getStatusAt());*/
			deliveryOrderService.handleInStorage(id);
		}
		return result;
	}
}
